<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class HouseLease extends Model
{
    protected $table = 'mini_house_lease';

    protected $guarded = [];

    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s'
    ];

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    //访问器
    public function getImageAttribute($value)
    {
        return asset($value);
    }

    public function getImagesAttribute($values)
    {
        $data = [];
        $images = explode(',', $values);
        foreach ($images as $value) {
            if ($value) {
                $data[] = asset($value);
            }
        }
        $data = implode(',', $data);
        return $data;
    }

    //关联小区
    public function community()
    {
        return $this->hasOne(Community::class, 'community_id', 'community_id');
    }
}
